package com.atguigu.boot.controller;

import com.atguigu.boot.pojo.Customer;
import com.atguigu.boot.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;

import static com.atguigu.boot.utils.PhoneUtils.getTel;

/**
 * spring-boot
 * 2022/9/14 00 22
 */
@RestController
@Slf4j
@Api("Customer数据库访问接口列表")
public class CustomerController {
    @Resource
    private CustomerService customerService;
    @ApiOperation ("添加一个新用户(需要填写@canme @age)。")
    @PostMapping("/customer/add")
    public String addCustomerController(Customer customer){
        customer.setPhone(getTel());
        customer.setSex((byte) new Random().nextInt(2));
        customer.setBirth( new Date ());
        customerService.add (customer);
        return "成功";
    }

    @ApiOperation ("查询所有消费者")
    @GetMapping("/customer/list")
    public List<Customer> list(){
        List<Customer> list = customerService.list ();
        System.out.println (list);
        return list;
    }

    @ApiOperation ("根据Id查询消费者")
    @GetMapping("/getCustomerById/{id}")
    public Customer getCustomerById(@PathVariable Integer id){
        return customerService.getCustomerById (id);
    }

    @ApiOperation ("根据Id删除消费者")
    @GetMapping("/deleteCustomerById/{id}")
    public void deleteCustomerById(@PathVariable Integer id){
        customerService.deleteCustomerById (id);
    }

    @ApiOperation ("更新员工的数据")
    @PostMapping("/updateCustomer")
    public void updateCustomer(Customer customer){
        customerService.updateCustomer (customer);
    }
}
